package com.kzx.blog.controller;

import com.kzx.blog.model.Comment;
import com.kzx.blog.model.Token;
import com.kzx.blog.service.IComment;
import com.kzx.blog.service.IUser;
import com.kzx.blog.service.model.CommentAddParam;
import com.kzx.blog.service.model.CommentPagingParam;
import com.kzx.blog.util.PageUtils;
import com.kzx.blog.util.ParseHttp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author kongz
 * @version 1.0 2021/12/14 9:38
 * @description 评论controller
 */
@RestController
@RequestMapping("/comment")
@Api(value = "CommentController", tags = {"评论接口"})
public class CommentController {

    private final IComment iComment;

    private final IUser iUser;

    public CommentController(IComment iComment, IUser iUser) {
        this.iComment = iComment;
        this.iUser = iUser;
    }

    @PostMapping("")
    @ApiOperation("新建博客评论")
    public int add(@RequestBody @Validated CommentAddParam commentAddParam) {
        return iComment.add(commentAddParam);
    }

    @PostMapping("/paging")
    @ApiOperation("分页查询评论信息")
    public PageUtils<Comment> paging(@RequestBody @Validated CommentPagingParam commentPagingParam, HttpServletRequest request) {
        if (commentPagingParam.getBlogId() > 0) {
            return iComment.paging(commentPagingParam, 0);
        }
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iComment.paging(commentPagingParam, user.getUid());
    }

    @PatchMapping("/{id}/{status}")
    public int pass(@PathVariable("id") int id, @PathVariable("status") int status, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iComment.pass(id, status, user);
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable("id") int id, HttpServletRequest request) {
        String token = ParseHttp.getHttpAccessToken(request);
        Token user = iUser.getToken(token);
        return iComment.delete(id, user);
    }

}
